package face.yuque;

/**
 * @author spf
 * @date 2024/12/11
 * @time 20:13
 */
//x的平方根
// 二分法
public class SqrtX2 {
    public static void main(String[] args) {
//        System.out.println(sqrtX(25));
        System.out.println(sqrtX(24));
    }

    private static int sqrtX(int x) {
        int index = -1, left = 0, right = x;
        while (left <= right) {
            int mid = (left + right) / 2;
            if (mid * mid <= x) {
                left = mid + 1;
                index = mid;
            } else {
                right = mid - 1;
            }
        }
        return index;
    }
}
